package com.users.rn.rncommon.util;

import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 11:34
 */
public class NoLeakHandler extends Handler {
    private WeakReference<HandlerCallback> mCallback = new WeakReference<HandlerCallback>(
            null);
    private boolean isValid = true;

    public NoLeakHandler(HandlerCallback callback) {
        mCallback = new WeakReference<HandlerCallback>(callback);
    }

    @Override
    public void handleMessage(Message msg) {
        if (mCallback.get() != null && isValid) {
            mCallback.get().handleMessage(msg);
        }
    }

    public interface HandlerCallback {
        public void handleMessage(Message msg);
    }

    public void setValid(boolean isValid) {
        this.isValid = isValid;
    }
}
